package com.wson.controller.clouddisk.framework;

import javax.servlet.http.HttpServletRequest;

import com.wson.adapter.clouddisk.factory.CodiskAdapterFactory;
import com.wson.adapter.codisk.ICodiskAdapter;
import com.wson.adapter.oauth.IOAuthAdapter;
import com.wson.constant.IUtilConstant;
import com.wson.controller.oauth.AbstractOAuthCertificationResponseController;
import com.wson.model.adapter.oauthaccount.IOAuthAccountManip;
import com.wson.model.adapter.oauthaccount.IOAuthAccountView;
import com.wson.model.clouddisk.account.ICodiskAccountManip;
import com.wson.model.clouddisk.account.ICodiskAccountMeta;
import com.wson.model.clouddisk.account.ICodiskAccountView;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.LoggerUtil;
import com.wson.util.element.SessionProfile;

public abstract class AbstractCodiskOAuthCertificationResponseController extends
		AbstractOAuthCertificationResponseController {

	protected IOAuthAccountView generateAccountCheckResult(IOAuthAccountView ecmaccount)throws Exception{
		ICodiskAccountView accountcheck = (ICodiskAccountView)BeanFactory.getCurrentFactory().getModelFactory().getModel(ICodiskAccountMeta.META_ID);
		ecmaccount.initModel(accountcheck);
		((ICodiskAccountManip)accountcheck).setProviderID(((IOAuthAccountView)ecmaccount).getProviderID());
		((ICodiskAccountManip)accountcheck).setAccessID(((IOAuthAccountView)ecmaccount).getAccessID());
		((ICodiskAccountManip)accountcheck).setCertified(true);
		((ICodiskAccountManip)accountcheck).setOauthToken(((IOAuthAccountView)ecmaccount).getOauthToken());
		((ICodiskAccountManip)accountcheck).setOauthSecret(((IOAuthAccountView)ecmaccount).getOauthSecret());
		
		accountcheck.selfSelect();
		LoggerUtil.DEBUG("check account "+accountcheck.getProviderID()+"  "+accountcheck.getAccessID());
		return accountcheck;
	}
	protected IOAuthAccountView prepareOAuthAccount(IOAuthAccountView account, HttpServletRequest request)throws Exception{
		ICodiskAccountView checkaccount = (ICodiskAccountView)account;
		LoggerUtil.DEBUG("model.getID() "+checkaccount.getID());
		if(checkaccount.getID()!=null 
				&& checkaccount.selfSelect()){
			//select successfully!			
		}
		else{
			SessionProfile sessionprofile = (SessionProfile)request.getSession().getAttribute(IUtilConstant.SESSION_PROFILE);
			((IOAuthAccountManip)checkaccount).setCurrentMatrixID(sessionprofile.getMatrixID());
			((IOAuthAccountManip)checkaccount).setProviderID(this.retrieveRequestParamLong(request, ICodiskAccountMeta.SYNC_PROVIDERID));
			((IOAuthAccountManip)checkaccount).setIdentityID(sessionprofile.getIdentity().getID());

		}
		return checkaccount;
	}
	
	@Override
	protected  IOAuthAdapter  getAdapter(IOAuthAccountView account, HttpServletRequest request){
		ICodiskAdapter adapter = CodiskAdapterFactory.getAdapter((ICodiskAccountView)account);
		
		return adapter;
	}
	
/*	@Override
	protected boolean acceptOAuthToken(IOAuthAccountView account,
			HttpServletRequest request, HttpServletResponse response)throws Exception {

		IOAuthAccountView ecmaccount = this.prepareCodiskAccount(account, request);
		
		LoggerUtil.DEBUG("model.getProviderID() "+ecmaccount.getProviderID());
		if(ecmaccount.getProviderID()!=null){
			
			//ICodiskAdaptor adapter = CodiskAdaptorFactory.getAdapter(ecmaccount);
			IOAuthAdaptor adapter = this.getAdaptor(ecmaccount, request);
			Map<String, Object> params = new HashMap<String, Object>();
			params.put(ICodiskAdaptor.REQUESTTOKEN, request.getParameter("code"));//request.getSession().getAttribute("code"));
			
			LoggerUtil.DEBUG("params "+params);
			
			//params.put(ICodiskAdapter.CALLBACKFUNCTION, request.getSession().getAttribute("code"));
			//params.put(ICodiskAdapter.VERIFIER, request.getParameter("oauth_verifier"));
			
			ecmaccount = (ICodiskAccountView)adapter.generateAccessAccount((ICodiskAccountView)ecmaccount, accountCertificationResponseAction(request) , params);
			
			IOAuthAccountView accountcheck = this.generateAccountCheckResult(ecmaccount);
			

			if(accountcheck.getID()==null){
				((IOAuthAccountManip)ecmaccount).selfCreate();
				//LoggerUtil.DEBUG("account agent "+ecmaccount.getAgentID());
				LoggerUtil.DEBUG("Create account "+ecmaccount.getID());
			}
			LoggerUtil.DEBUG("check result "+accountcheck.getID()+"  "+((IOAuthAccountView)ecmaccount).getOauthToken());
			//adapter.retrieveAccess(model, params);
			
			String  redirecturl = BeanFactory.getCurrentFactory().getControllerFactory().generateURL(IControllerFactory.ACTION_MODELRESPONSE)//IEagentActionConstant.ACTION_MODELRESPONSE
					+"?"
					+ HttpUtil.generateHttpParam(IRender.PARAM_RENDERCODE, IRenderConstant.RENDERCODE_WORKCENTER)
					//+ HttpUtil.generateHttpParam(ICodiskAgentMeta.SYNC_ID, account.getID())
					//+ HttpUtil.generateHttpParam(ICodiskAgentMeta.SYNC_METAID, ICodiskAgentMeta.META_ID)
					;
			response.sendRedirect(redirecturl);
			
		}
		
		return true;
	}*/

	

/*	protected  boolean isAccountExisted(IOAuthAdaptor adapter, Map<String, Object> params )throws Exception{

		IOAuthAccountView accessaccount = (IOAuthAccountView)adapter.generateAccessAccount(accountCertificationResponseAction(null), params);
		
		ICodiskAccountView accountcheck = (ICodiskAccountView)BeanFactory.getCurrentFactory().getModelFactory().getModel(ICodiskAccountMeta.META_ID);
		accessaccount.initModel(accountcheck);
		((ICodiskAccountManip)accountcheck).setProviderID(accessaccount.getProviderID());
		((ICodiskAccountManip)accountcheck).setAccessID(accessaccount.getAccessID());
		((ICodiskAccountManip)accountcheck).setCertified(true);
		((ICodiskAccountManip)accountcheck).setOauthToken(accessaccount.getOauthToken());
		((ICodiskAccountManip)accountcheck).setOauthSecret(accessaccount.getOauthSecret());
		
		
		LoggerUtil.DEBUG("check account "+accountcheck.getProviderID()+"  "+accountcheck.getAccessID());
		accountcheck.selfSelect();
		
		LoggerUtil.DEBUG("check result "+accountcheck.getID());
		
		return accountcheck.getID()!=null;
	}*/

}
